package com.mz.ozy.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.ShogListAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.ShogBean;
import com.mz.ozy.bean.ShogDetailBean;
import com.mz.ozy.mvp.presenter.ShogaPresenter;
import com.mz.ozy.mvp.view.ShogaContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lifeStyle on 2020/4/21 0021.
 */
public class ShogakuinActivity extends BaseActivity<ShogaContract.View, ShogaPresenter> implements ShogaContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private int pageNo = 1;
    private int tag = 0;
    private ShogListAdapter mAdapter;
    private List<ShogBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopakuin);
        ButterKnife.bind(this);
        initTitle();
        final String title = getIntent().getStringExtra("title");
        tvCenterTitle.setText(title);
        initView();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ShogListAdapter(mBeanList);
        recyclerview.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ShogBean.PageBean.PageItemsBean dataa = mAdapter.getItem(position);
                Intent intent = new Intent(ShogakuinActivity.this, ShogDetailActivity.class);
                intent.putExtra("id", dataa.getId() + "");
                startActivity(intent);
            }
        });

        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getInfo(ShogakuinActivity.this, pageNo + "");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getInfo(ShogakuinActivity.this, pageNo + "");
            }
        });


    }

    @Override
    public void getInfo(ShogBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void getInfoDetailSuccess(ShogDetailBean homeMerBean) {

    }
}
